package geektime.spring.autoconfiguredemo;

import geektime.spring.autoconfiguredemo.autotest.TestController;
import geektime.spring.autoconfiguredemo.controller.UserController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;


@SpringBootApplication
@Slf4j
@EnableAutoConfiguration

public class AutoconfigureDemoApplication implements ApplicationRunner {


	public static void main(String[] args) {
		SpringApplication.run(AutoconfigureDemoApplication.class, args);
	}


	@Override
	public void run(ApplicationArguments args) throws Exception {
		//xml装配
		xmlTest();
		//Annotation装配
		annotationTest();
		//autoWire装配
		autoWireTest();


	}

	public void  xmlTest(){
		String xmlPath = "Coffe.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);
		log.info("Coffe基于XML的装配 :{}",ac.getBean("Latie"));
		log.info("Coffe通过构造器注入 :{}",ac.getBean("cappuccino"));
		log.info("Coffe通过设置值注入 :{}",ac.getBean("milktea"));
		log.info("---Bean基于XMl装配：{}","测试成功---");
	}

	public void annotationTest(){
		String xmlPath = "Annotation.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);
		UserController uc = (UserController)ac.getBean("userControllerA");
		uc.save();
		log.info("---Bean基于Annotation注解：{}","测试成功---");
	}

	public  void autoWireTest(){
		//获取XML配置文件路径
		String xmlPath = "Autowire.xml";
		//创建容器加载配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);
		//获取实例赋值给uc
		TestController uc = (TestController)ac.getBean("testController");
		//调用uc里面的save()方法
		uc.save();
		log.info("----Bean基于自动装配：{}","测试成功---");
	}

}
